﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Diagnostics;
using framework.map;

namespace framework.ui
{
    public partial class Form1 : Form
    {

        // GLOBAL STATICS
        MapFile last = new MapFile();
        SliderState slst = new SliderState();

        SetPath sp = new SetPath();
        string[] filenames = new string[50];
        public Form1()
        {
            InitializeComponent();
            this.AllowDrop = true;
            this.DragEnter += new DragEventHandler(Form1_DragEnter);
            this.DragDrop += new DragEventHandler(Form1_DragDrop);

            sp.Visible = false;
            this.Controls.Add(sp);
        }

        void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
        }

        void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            foreach (string file in files)
            {
                richTextBox1.Text = File.ReadAllText(file);
                build(file, false);
                last = new MapFile(file);
            }
        }

        private void build(string file, bool updating)
        {
            if (Path.GetExtension(file) == ".map")
            {
                ProcessStartInfo info = new ProcessStartInfo();
                info.WindowStyle = ProcessWindowStyle.Hidden;
                info.UseShellExecute = true;
                info.Arguments = file +
                    " \"" +
                    Properties.Settings.Default.data_path +
                    "\"";
                info.FileName = Properties.Settings.Default.mapc_path;

                try
                {

                    Process p = new Process();
                    p.StartInfo = info;
                    p.Start();
                    this.toolStripStatusLabel1.Text = "compiling...";
                    p.WaitForExit();
                    if (!updating)
                        this.toolStripStatusLabel1.Text = "Map has been successfully built";
                    else
                        this.toolStripStatusLabel1.Text = "Map has been successfully updated";
                }
                catch (Exception exc)
                {
                    doNothing(exc.Message);
                    this.toolStripStatusLabel1.Text = "Cannot build, sorry";
                }
            }
        }


        #region Events

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        void doNothing(string exc) { }

        private void viewPathEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (button3.Text == "Set Paths")
            {
                this.button3.Text = "View Compiler";
                this.groupBox1.Visible = false;
                this.sp.Visible = true;
            }
            else if(button3.Text == "View Compiler")
            {
                this.button3.Text = "Set Paths";
                this.groupBox1.Visible = true;
                this.sp.Visible = false;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            viewPathEditorToolStripMenuItem_Click(sender, e);
        }

        #endregion

        private void button4_Click(object sender, EventArgs e)
        {
            build(last.Path, true);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled == false) timer1.Enabled = true;
            slst = SliderState.Close;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled == false) timer1.Enabled = true;
            if (button1.Text == "Show Content")
            {
                button1.Text = "Don't Show Content";
                slst = SliderState.Down;
            }
            else
            {
                button1.Text = "Show Content";
                slst = SliderState.Up;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (slst == SliderState.Down)
            {
                if (this.Height > 450)
                    timer1.Enabled = false;
                this.Height += 12;
            }
            else if (slst == SliderState.Up)
            {
                if (this.Height < 360)
                    timer1.Enabled = false;
                this.Height -= 12;
            }
            else if (slst == SliderState.Close)
            {
                if (this.Height < 1)
                    this.Close();
                this.Height -= 22;
            }
        
        }

    }
}
